package com.ctsi.system.workcalendar.defdate;

import com.ctsi.commons.util.UtilValidate;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * @author zhangjw
 * @className Dates
 * @description
 * @date 2019/3/30 9:21
 **/
public enum FreeDateUtil {

    INST;

    private List<Date> date;

    FreeDateUtil() {
        date = new ArrayList<>();
        // type 0阳历 1阴历
        // 周六 周日
        Date week = new Date();
        week.setWeek("7");
        week.setType("0");

        Date week1 = new Date();
        week1.setWeek("1");
        week1.setType("0");

        date.add(week);
        date.add(week1);
        //中国传统节假日
        // 元旦
        Date h1 = new Date();
        h1.setMonth("1");
        h1.setDay("1");
        h1.setType("0");
        date.add(h1);
        // 五一
        Date h2 = new Date();
        h2.setMonth("5");
        h2.setDay("1");
        h2.setType("0");
        date.add(h2);

        Date h3 = new Date();
        h3.setMonth("5");
        h3.setDay("2");
        h3.setType("0");
        date.add(h3);

        Date h4 = new Date();
        h4.setMonth("5");
        h4.setDay("3");
        h4.setType("0");
        date.add(h4);
        // 十月一
        Date h5 = new Date();
        h5.setMonth("10");
        h5.setDay("1");
        h5.setType("0");
        date.add(h5);

        Date h6 = new Date();
        h6.setMonth("10");
        h6.setDay("2");
        h6.setType("0");
        date.add(h6);

        Date h7 = new Date();
        h7.setMonth("10");
        h7.setDay("3");
        h7.setType("0");
        date.add(h7);

        Date h8 = new Date();
        h8.setMonth("10");
        h8.setDay("4");
        h8.setType("0");
        date.add(h8);

        Date h9 = new Date();
        h9.setMonth("10");
        h9.setDay("5");
        h9.setType("0");
        date.add(h9);

        Date h10 = new Date();
        h10.setMonth("10");
        h10.setDay("6");
        h10.setType("0");
        date.add(h10);

        Date h11 = new Date();
        h11.setMonth("10");
        h11.setDay("7");
        h11.setType("0");
        date.add(h11);

        // 中秋
        Date h12 = new Date();
        h12.setMonth("8");
        h12.setDay("15");
        h12.setType("1");
        date.add(h12);
        // 端午
        Date h13 = new Date();
        h13.setMonth("5");
        h13.setDay("5");
        h13.setType("1");
        date.add(h13);
        // 春节
        Date h14 = new Date();
        h14.setMonth("1");
        h14.setDay("1");
        h14.setType("1");
        date.add(h14);

        Date h15 = new Date();
        h15.setMonth("1");
        h15.setDay("2");
        h15.setType("1");
        date.add(h15);

        Date h16 = new Date();
        h16.setMonth("1");
        h16.setDay("3");
        h16.setType("1");
        date.add(h16);

        Date h17 = new Date();
        h17.setMonth("1");
        h17.setDay("4");
        h17.setType("1");
        date.add(h17);

        Date h18 = new Date();
        h18.setMonth("1");
        h18.setDay("5");
        h18.setType("1");
        date.add(h18);

        Date h19 = new Date();
        h19.setMonth("1");
        h19.setDay("6");
        h19.setType("1");
        date.add(h19);
    }

    public List<Date> getDate() {
        return date;
    }

    public boolean isFree(Calendar c) {
        int y = c.get(Calendar.YEAR);
        int m = c.get(Calendar.MONDAY) + 1;
        int d = c.get(Calendar.DAY_OF_MONTH);
        int w = c.get(Calendar.DAY_OF_WEEK);

        Lunar lunar = new Lunar();
        lunar.Lunar1(c.getTime());

        int oy = lunar.getYear();
        int om = lunar.getMonCyl();
        int od = lunar.getDay();

        for (int i = 0; i < date.size(); i++) {
            Date dt = date.get(i);
            if (UtilValidate.isNotEmpty(dt.getType()) && "1".equals(dt.getType())) {
                // 农历
                if (
                        (UtilValidate.isEmpty(dt.getYear()) || (UtilValidate.isNotEmpty(dt.getYear()) && String.valueOf(oy).equals(dt.getYear())))
                        &&
                        (UtilValidate.isEmpty(dt.getMonth()) || (UtilValidate.isNotEmpty(dt.getMonth()) && String.valueOf(om).equals(dt.getMonth())))
                        &&
                        (UtilValidate.isEmpty(dt.getDay()) || (UtilValidate.isNotEmpty(dt.getDay()) && String.valueOf(od).equals(dt.getDay())))) {
                    return true;
                }
            } else {
                // 公历
                if (
                        (UtilValidate.isEmpty(dt.getYear()) || (UtilValidate.isNotEmpty(dt.getYear()) && String.valueOf(y).equals(dt.getYear())))
                        &&
                        (UtilValidate.isEmpty(dt.getMonth()) || (UtilValidate.isNotEmpty(dt.getMonth()) && String.valueOf(m).equals(dt.getMonth())))
                        &&
                        (UtilValidate.isEmpty(dt.getDay()) || (UtilValidate.isNotEmpty(dt.getDay()) && String.valueOf(d).equals(dt.getDay())))
                        &&
                        (UtilValidate.isEmpty(dt.getWeek()) || (UtilValidate.isNotEmpty(dt.getWeek()) && String.valueOf(w).equals(dt.getWeek())))
                ) {
                    return true;
                }

            }
        }

        return false;
    }
}
